package com.ruoyi.template.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.template.domain.bo.VtsTemplateCodeBo;
import com.ruoyi.template.domain.vo.VtsTemplateCodeVo;
import com.ruoyi.template.domain.VtsTemplateCode;
import com.ruoyi.template.mapper.VtsTemplateCodeMapper;
import com.ruoyi.template.service.IVtsTemplateCodeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 表单代码Service业务层处理
 *
 * @author fjw
 * @date 2023-08-25
 */
@RequiredArgsConstructor
@Service
public class VtsTemplateCodeServiceImpl implements IVtsTemplateCodeService {

    private final VtsTemplateCodeMapper baseMapper;

    /**
     * 查询表单代码
     */
    @Override
    public VtsTemplateCodeVo queryById(Long formId){
        return baseMapper.selectVoById(formId);
    }

    /**
     * 查询表单代码列表
     */
    @Override
    public TableDataInfo<VtsTemplateCodeVo> queryPageList(VtsTemplateCodeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<VtsTemplateCode> lqw = buildQueryWrapper(bo);
        Page<VtsTemplateCodeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询表单代码列表
     */
    @Override
    public List<VtsTemplateCodeVo> queryList(VtsTemplateCodeBo bo) {
        LambdaQueryWrapper<VtsTemplateCode> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<VtsTemplateCode> buildQueryWrapper(VtsTemplateCodeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<VtsTemplateCode> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getFormName()), VtsTemplateCode::getFormName, bo.getFormName());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), VtsTemplateCode::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), VtsTemplateCode::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增表单代码
     */
    @Override
    public Boolean insertByBo(VtsTemplateCodeBo bo) {
        VtsTemplateCode add = BeanUtil.toBean(bo, VtsTemplateCode.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setFormId(add.getFormId());
        }
        return flag;
    }

    /**
     * 修改表单代码
     */
    @Override
    public Boolean updateByBo(VtsTemplateCodeBo bo) {
        VtsTemplateCode update = BeanUtil.toBean(bo, VtsTemplateCode.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(VtsTemplateCode entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除表单代码
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
